package com.aemreunal.config; /* * *********************** * * Copyright (c) 2015 * * * * This code belongs to: * * * * @author Ahmet Emre Ünal * * S001974 * * * * aemreunal@gmail.com * * emre.unal@ozu.edu.tr * * * * aemreunal.com * * *********************** * */ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; /* * http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-configuration/ */ @Configuration @EnableWebMvc @ComponentScan(GlobalSettings.BASE_PACKAGE_NAME) public class MVCConfig extends WebMvcConfigurerAdapter { @Autowired public MappingJackson2HttpMessageConverter jacksonMessageConverter; @Autowired public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter; @Autowired public StringHttpMessageConverter stringHttpMessageConverter; // @Autowired // private SessionFactory sessionFactory; @Bean public MappingJackson2HttpMessageConverter jacksonMessageConverter() { MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = new ObjectMapper(); //Registering Hibernate4Module to support lazy objects mapper.registerModule(new Hibernate4Module()); messageConverter.setObjectMapper(mapper); return messageConverter; } @Bean public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter(); } @Bean public StringHttpMessageConverter stringHttpMessageConverter() { return new StringHttpMessageConverter(); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // Add our custom-configured HttpMessageConverters converters.add(jacksonMessageConverter); converters.add(byteArrayHttpMessageConverter); converters.add(stringHttpMessageConverter); super.configureMessageConverters(converters); } // @Override // public void addInterceptors(InterceptorRegistry registry) { // super.addInterceptors(registry); // OpenSessionInViewInterceptor osiv = new OpenSessionInViewInterceptor(); // osiv.setSessionFactory(sessionFactory); // registry.addWebRequestInterceptor(osiv); // } }